<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class Page_lib
 * @author Davinder
 * @version 1.0
 */
 
class User_lib
{
    private $_CI;
    private $_picture_config;
    
    public function __construct() {
        $this->_CI = & get_instance();
        log_message('ERROR',__CLASS__.' Class Initialized');
        $this->_CI->load->model('user_model');
        
        $this->_CI->config->load('scrapm');
        $scrapm_config = $this->_CI->config->item('scrapm');
        $this->picture_config = $scrapm_config['picture_config'];
	}
    
    public function save_user_login_data($p_data=null) {
        
        $user_info = $this->_CI->user_model->save_user_login_data($p_data);
        
        if ( isset($user_info) && is_object($user_info) && $user_info->returning_user == false ) {
            
            $user_id = $user_info->user_id;
            
            $this->_CI->config->load('scrapm');
            $scrapm_config = $this->_CI->config->item('scrapm');
            
            $this->_CI->load->library('upload', $scrapm_config['picture_config']);
            
            if ( empty($p_data->profile_img) ) {
                
                $this->_CI->upload->do_upload('picture');
                
                $p_data->uploaded_file_data = $this->_CI->upload->data();
                
                $ext = $p_data->uploaded_file_data['file_ext'];
                $source = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$p_data->uploaded_file_data['raw_name'].$ext;
                
                $destination_filename = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$ext;
                $destination = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$destination_filename;
                
                rename($source,$destination);
                
                $config['image_library']    = 'gd2';
                $config['source_image']	    = $destination;
                $config['create_thumb']     = TRUE;
                $config['thumb_marker']     = $this->picture_config['profile_name_suffix_thumbnail'];
                $config['maintain_ratio']   = $this->picture_config['maintain_ratio'];
                $config['width']	        = $this->picture_config['profile_thumbnail_width'];
                $config['height']	        = $this->picture_config['profile_thumbnail_height'];
                
                $config2['image_library']       = 'gd2';
                $config2['source_image']	    = $destination;
                $config2['create_thumb']        = TRUE;
                $config2['thumb_marker']        = $this->picture_config['profile_name_suffix_intermediate'];
                $config2['maintain_ratio']      = $this->picture_config['maintain_ratio'];
                $config2['width']	            = $this->picture_config['profile_intermediate_width'];
                $config2['height']	            = $this->picture_config['profile_intermediate_height'];
                
                $this->_CI->load->library('image_lib'); 
                
                $this->_CI->image_lib->initialize($config);
                $this->_CI->image_lib->resize();
                $this->_CI->image_lib->initialize($config2);
                $this->_CI->image_lib->resize();
                
                $p_data->thumbnail = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_thumbnail'].$ext;
                $p_data->intermediate = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_intermediate'].$ext;
                $p_data->fullimage = $destination_filename;
                $p_data->user_id = $user_id;
                
                $this->_CI->user_model->move_and_update_picture($p_data);    
            
            } else {
    
                $ext = '.'.pathinfo($p_data->profile_img,PATHINFO_EXTENSION);
                $file = file_get_contents($p_data->profile_img);
                
                if ( !empty($file) ) {
                    
                    $imagename = 'profile_picture_%d.'.$ext;
                    $imagename = sprintf($imagename,$user_id);
                    $source = $this->picture_config['upload_path'].$imagename;
                    file_put_contents($source,$file);
                    
                    $destination_filename = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$ext;
                    $destination = $this->picture_config['upload_path'].DIRECTORY_SEPARATOR.$destination_filename;
                    
                    rename($source,$destination);
                    
                    $config['image_library']    = 'gd2';
                    $config['source_image']	    = $destination;
                    $config['create_thumb']     = TRUE;
                    $config['thumb_marker']     = $this->picture_config['profile_name_suffix_thumbnail'];
                    $config['maintain_ratio']   = $this->picture_config['maintain_ratio'];
                    $config['width']	        = $this->picture_config['profile_thumbnail_width'];
                    $config['height']	        = $this->picture_config['profile_thumbnail_height'];
                    
                    $config2['image_library']       = 'gd2';
                    $config2['source_image']	    = $destination;
                    $config2['create_thumb']        = TRUE;
                    $config2['thumb_marker']        = $this->picture_config['profile_name_suffix_intermediate'];
                    $config2['maintain_ratio']      = $this->picture_config['maintain_ratio'];
                    $config2['width']	            = $this->picture_config['profile_intermediate_width'];
                    $config2['height']	            = $this->picture_config['profile_intermediate_height'];
                    
                    $this->_CI->load->library('image_lib'); 
                    
                    $this->_CI->image_lib->initialize($config);
                    $this->_CI->image_lib->resize();
                    $this->_CI->image_lib->initialize($config2);
                    $this->_CI->image_lib->resize();
                    
                    $p_data->thumbnail = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_thumbnail'].$ext;
                    $p_data->intermediate = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_intermediate'].$ext;
                    $p_data->fullimage = $destination_filename;
                    $p_data->user_id = $user_id;
                    
                    $this->_CI->user_model->move_and_update_picture($p_data);
                }
            }
            
            return $user_info;
        } else {
            return $user_info;
        }
    }
    
    public function get_user_profile_data($p_data=null) {
        
        $user_profile_data = $this->_CI->user_model->get_user_profile_data($p_data);
        
        if ( !empty($user_profile_data) ) {
            $user_profile_data->user_id = $user_profile_data->id;
            $user_profile_data->id = $user_profile_data->facebook_idn;
            $user_profile_data->email = $user_profile_data->email_address;
            
            if ( $user_profile_data->flag_is_profile_image_facebook == 1 ) {
                $user_profile_data->profile_thumbnail = null;
                $user_profile_data->profile_mainimage = null;
                $user_profile_data->profile_img = $user_profile_data->profile_image_url;
            } else {
                
                $this->_CI->config->load('scrapm');
                $scrapm_config = $this->_CI->config->item('scrapm');
                $this->picture_config = $scrapm_config['picture_config'];
                
                $user_profile_data->profile_img = null;
                $user_profile_data->profile_thumbnail = site_url().$this->picture_config['upload_folder'].'/'.$user_profile_data->profile_thumbnail;
                $user_profile_data->profile_mainimage = site_url().$this->picture_config['upload_folder'].'/'.$user_profile_data->profile_mainimage;
            }
            
            $user_profile_data->academic = new stdClass();
            $user_profile_data->school = new stdClass();
            $user_profile_data->academic->id = $user_profile_data->academic_id;
            $user_profile_data->academic->name = $user_profile_data->academic_name;
            
            $user_profile_data->school->id = $user_profile_data->profile_school_id;
            $user_profile_data->school->name = $user_profile_data->school_name;
            
            unset($user_profile_data->profile_school_id);
            unset($user_profile_data->profile_user_id);
            unset($user_profile_data->facebook_idn);
            unset($user_profile_data->email_address);
            unset($user_profile_data->profile_image_url);
            unset($user_profile_data->academic_id);
            unset($user_profile_data->academic_name);
            unset($user_profile_data->profile_academic_year);
            unset($user_profile_data->academic_is_current);
        }
        
        return $user_profile_data;
    }
    
    public function update_user_login_data($p_data=null) {
        
        if ( count($p_data) ) {
            foreach( $p_data as $p_key=>$p_value ) {
                if ( empty($p_data->$p_key) ) {
                    unset($p_data->$p_key);
                }
            }
        }
        
        $user_id = $p_data->user_id;
        
        $this->_CI->config->load('scrapm');
        $scrapm_config = $this->_CI->config->item('scrapm');
        $this->_CI->load->library('upload', $scrapm_config['picture_config']);
        
        $p_data->flag_is_profile_image_facebook = 0;
       
        if ( empty($p_data->profile_img) ) {
            
            if ( $this->_CI->upload->do_upload('picture') ) {
                
                $p_data->uploaded_file_data = $this->_CI->upload->data();
                
                $ext = $p_data->uploaded_file_data['file_ext'];
                $source = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$p_data->uploaded_file_data['raw_name'].$ext;
                
                $destination_filename = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$ext;
                $destination = $p_data->uploaded_file_data['file_path'].DIRECTORY_SEPARATOR.$destination_filename;
                
                rename($source,$destination);
                
                $config['image_library']    = 'gd2';
                $config['source_image']	    = $destination;
                $config['create_thumb']     = TRUE;
                $config['thumb_marker']     = $this->picture_config['profile_name_suffix_thumbnail'];
                $config['maintain_ratio']   = $this->picture_config['maintain_ratio'];
                $config['width']	        = $this->picture_config['thumbnail_width'];
                $config['height']	        = $this->picture_config['thumbnail_height'];
                
                $config2['image_library']       = 'gd2';
                $config2['source_image']	    = $destination;
                $config2['create_thumb']        = TRUE;
                $config2['thumb_marker']        = $this->picture_config['profile_name_suffix_intermediate'];
                $config2['maintain_ratio']      = $this->picture_config['maintain_ratio'];
                $config2['width']	            = $this->picture_config['intermediate_width'];
                $config2['height']	            = $this->picture_config['intermediate_height'];
                
                $this->_CI->load->library('image_lib'); 
                
                $this->_CI->image_lib->initialize($config);
                $this->_CI->image_lib->resize();
                $this->_CI->image_lib->initialize($config2);
                $this->_CI->image_lib->resize();
                
                $p_data->thumbnail = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_thumbnail'].$ext;
                $p_data->intermediate = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_intermediate'].$ext;
                $p_data->fullimage = $destination_filename;
                $p_data->user_id = $user_id;
                
                $this->_CI->user_model->move_and_update_picture($p_data);
                $p_data->flag_is_profile_image_facebook = 0; 
            }
            
            $user_info = $this->_CI->user_model->update_user_login_data($p_data);
            
            return $user_info;
        } else {
    
            $ext = '.'.pathinfo($p_data->profile_img,PATHINFO_EXTENSION);
            $file = file_get_contents($p_data->profile_img);
            
            if ( !empty($file) ) {
                
                $imagename = 'profile_picture_%d.'.$ext;
                $imagename = sprintf($imagename,$user_id);
                $source = $this->picture_config['upload_path'].$imagename;
                file_put_contents($source,$file);
                
                $destination_filename = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$ext;
                $destination = $this->picture_config['upload_path'].DIRECTORY_SEPARATOR.$destination_filename;
                
                rename($source,$destination);
                
                $config['image_library']    = 'gd2';
                $config['source_image']	    = $destination;
                $config['create_thumb']     = TRUE;
                $config['thumb_marker']     = $this->picture_config['profile_name_suffix_thumbnail'];
                $config['maintain_ratio']   = $this->picture_config['maintain_ratio'];
                $config['width']	        = $this->picture_config['profile_thumbnail_width'];
                $config['height']	        = $this->picture_config['profile_thumbnail_height'];
                
                $config2['image_library']       = 'gd2';
                $config2['source_image']	    = $destination;
                $config2['create_thumb']        = TRUE;
                $config2['thumb_marker']        = $this->picture_config['profile_name_suffix_intermediate'];
                $config2['maintain_ratio']      = $this->picture_config['maintain_ratio'];
                $config2['width']	            = $this->picture_config['profile_intermediate_width'];
                $config2['height']	            = $this->picture_config['profile_intermediate_height'];
                
                $this->_CI->load->library('image_lib'); 
                
                $this->_CI->image_lib->initialize($config);
                $this->_CI->image_lib->resize();
                $this->_CI->image_lib->initialize($config2);
                $this->_CI->image_lib->resize();
                
                $p_data->thumbnail = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_thumbnail'].$ext;
                $p_data->intermediate = $this->picture_config['profile_name_prefix_fullimage'].'_'.$user_id.$this->picture_config['profile_name_suffix_intermediate'].$ext;
                $p_data->fullimage = $destination_filename;
                $p_data->user_id = $user_id;;
                
            }
            
            $p_data->profile_img = null;
            $this->_CI->user_model->move_and_update_picture($p_data);
            $user_info = $this->_CI->user_model->update_user_login_data($p_data);
            
            return $user_info;
        }
    }

    public function update_user_status($p_data=null) {
        if ( !empty($p_data->user_id) ) {
            
            $user_info = $this->_CI->user_model->update_user_status($p_data);
            return $user_info;
        }
    }
    
    public function get_selfies_details($p_data=null) {
        
        $user_data = $this->_CI->user_model->get_user_profile_data_by_userid($p_data);
        
        if ( !empty($user_data) ) {
            $p_data->school_id = $user_data->profile_school_id;
            $selfiesData = $this->_CI->user_model->get_selfies_details($p_data);
            
            if ( !empty($selfiesData['resultSet']) ) {
                
                $selfiesDataSet = array();
                $i = 0;
                
                $this->_CI->config->load('scrapm');
                $scrapm_config = $this->_CI->config->item('scrapm');
                $sitepath = site_url().$scrapm_config['picture_config']['upload_folder'].'/';
                
                foreach ( $selfiesData['resultSet'] as $selfiesDataElement ) {
                    
                    $selfiesDataSet[$i] = $selfiesDataElement;
                   
                    $selfiesDataSet[$i]->profile_thumbnail = (!empty($selfiesDataElement->profile_thumbnail)) ? $sitepath.$selfiesDataElement->profile_thumbnail : null;
                    $selfiesDataSet[$i]->profile_mainimage = (!empty($selfiesDataElement->profile_mainimage)) ? $sitepath.$selfiesDataElement->profile_mainimage : null;
                    $selfiesDataSet[$i]->profile_intermediate = (!empty($selfiesDataElement->profile_intermediate)) ? $sitepath.$selfiesDataElement->profile_intermediate : null;
                    
                    unset($selfiesDataSet[$i]->profile_image_url);
                    $i++;
                }
                
                return $selfiesDataSet;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
}